package moltencore.game.loader;

import moltencore.game.loader.Level;
import moltencore.game.loader.LevelParser;
import org.xml.sax.XMLReader;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.XMLReaderFactory;
import spark.exception.LoadingException;

import java.io.IOException;

public class LevelLoader {
	public static Level loadLevel(String levelPath, LevelParser parsingStrategy) throws LoadingException {
		try {
			XMLReader reader = XMLReaderFactory.createXMLReader();
			reader.setContentHandler(parsingStrategy);
			reader.parse(levelPath);
			return parsingStrategy.getParsedLevel();
		}
		catch(IOException e) {
			throw new LoadingException(e);
		}
		catch(SAXException e) {
			throw new LoadingException(e);
		}
	}
}
